package com.wis.controller;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.wis.entity.BannerEntity;
import com.wis.service.BannerService;
import com.wis.util.CommonResult;
import com.wis.util.UploadPictureUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/admin/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private UploadPictureUtil uploadPictureUtil;

    @PostMapping("/addBanner")
    public CommonResult addBanner(@RequestParam("bannerJson") String bannerJson,
                                  @RequestParam("file") MultipartFile file) {
        Gson gson = new Gson();
        BannerEntity banner = gson.fromJson(bannerJson, BannerEntity.class);
        String upload = uploadPictureUtil.upload(file);
        banner.setImg(upload);
        return bannerService.addBanner(banner);
    }
    @PostMapping("/updateBanner")
    public CommonResult updateBanner(@RequestBody BannerEntity banner) {
        return bannerService.updateBanner(banner);
    }
    @PostMapping("/updateBannerImg")
    public CommonResult updateBannerImg(@RequestParam("id") Integer id,
                                        @RequestParam("file") MultipartFile file) {
        BannerEntity banner = new BannerEntity();
        banner.setId(id);
        String upload = uploadPictureUtil.upload(file);
        banner.setImg(upload);
        return bannerService.updateBannerImg(banner);
    }
    @PostMapping("/delBanner/{id}")
    public CommonResult delBanner(@PathVariable("id") int id) {
        return bannerService.delBanner(id);
    }
    @GetMapping("/showAll")
    public CommonResult showAll() {
        return bannerService.showAll();
    }

    @GetMapping("/showByName")
    public CommonResult showByName(@RequestParam("name") String name) {
        return bannerService.showByName(name);
    }
}
